package com.mz.ieann.aide.utils

import android.content.Context
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.FlutterEngineCache
import io.flutter.embedding.engine.dart.DartExecutor

/**
 * <p>------------------------------------------------------
 * <p>Copyright (C) 2020 wasu company, All rights reserved.
 * <p>------------------------------------------------------
 * <p> Flutter 相关协助类
 * <p> 可执行代码为dart语言，FlutterEngine主要将dart语音以Flutter框架为媒介，在android系统执行
 * <p> FlutterEngine可以在任意时候执行任意代码，即绘制UI，网络请求，数据缓存等
 * <p>
 *     1、每次跳转使用 FlutterActivity.withNewEngine，需要新创建一个 FlutterEngine，会导致跳转卡顿，慢
 *     2、FlutterEngine 是可以在Android应用程序中运行Dart代码的容器
 *     3、dart代码可以在后台运行，也可以将dart编好后使用flutter框架渲染到屏幕上
 *     4、添加 FlutterEngine 缓存原因  a:提高运行时的性能
 *                                  b:FlutterEngine可以独立于FlutterActivity执行dart代码，即非UI下，也可以执行网路请求
 *     5、因为可独立运行，所以当UI销毁时，记得 FlutterEngine.destroy
 *
 * @author Created by zhangmz
 * @date on 2023/7/4
 */
object FlutterHelper {

    private lateinit var flutterEngine: FlutterEngine

    private const val cachedEngineId = "aaa"

    /**
     * 创建一个 FlutterEngine 缓存，缓存保存在map中
     */
    fun initEngineCache(
        context: Context
    ){
        // Instantiate a FlutterEngine.
        flutterEngine = FlutterEngine(context).apply {
            // Configure an initial route.
            // 设置初始路由后，会调用main.dart中的runApp，此时再修改导航路由，想要在不同的activity或fragment中使用同一个FlutterEngine将不会生效
            navigationChannel.setInitialRoute("")
            // Start executing Dart code to pre-warm the FlutterEngine.
            dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault())
        }
        // Cache the FlutterEngine to be used by FlutterActivity.
        FlutterEngineCache.getInstance().put(cachedEngineId, flutterEngine)
    }


    fun destroyEngine(){
        flutterEngine.destroy()
    }

    fun startNewEngine(
        context: Context
    ){
        runCatching {
            context.startActivity(FlutterActivity.withNewEngine().build(context))
        }.onFailure { it.printStackTrace() }
    }

    fun startCacheEngine(
        context: Context,
        route: String? = null
    ){
        runCatching {
            context.startActivity(
                when{
                    route.isNullOrEmpty() -> FlutterActivity.withCachedEngine(cachedEngineId).build(context)
                    else -> FlutterActivity.withNewEngine().initialRoute(route).build(context)
                }
            )
        }.onFailure { it.printStackTrace() }
    }


    fun initFlutterBoost(context: Context){
//        FlutterBoost.instance().setup(context, new FlutterBoostDelegate() {
//            @Override
//            public void pushNativeRoute(FlutterBoostRouteOptions options) {
//                //这里根据options.pageName来判断你想跳转哪个页面，这里简单给一个
//                Intent intent = new Intent(FlutterBoost.instance().currentActivity(), YourTargetAcitvity.class);
//                FlutterBoost.instance().currentActivity().startActivityForResult(intent, options.requestCode());
//            }
//
//            @Override
//            public void pushFlutterRoute(FlutterBoostRouteOptions options) {
//                Intent intent = new FlutterBoostActivity.CachedEngineIntentBuilder(FlutterBoostActivity.class)
//                    .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
//                    .destroyEngineWithActivity(false)
//                    .uniqueId(options.uniqueId())
//                    .url(options.pageName())
//                    .urlParams(options.arguments())
//                    .build(FlutterBoost.instance().currentActivity());
//                FlutterBoost.instance().currentActivity().startActivity(intent);
//            }
//        }, engine -> {
//        });
    }

}